import java.util.ArrayList;
import java.util.List;

public class test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.addAll(new ArrayList<>());
    }
}
class Solution {
    static class TreeNode{
        TreeNode left;
        TreeNode right;
        int val;
    }
    int[] num = new int[101];
    private void dfs(TreeNode root){
        if(root == null) return;
        num[root.val] = root.val;
        dfs(root.right);
        dfs(root.left);
    }
    private void create(TreeNode root){
        if(root==null) return;
        root.val = num[root.val];
        create(root.right);
        create(root.left);
    }
    public TreeNode bstToGst(TreeNode root) {
        dfs(root);
        for(int i=99; i>=0; i--){
            num[i] += num[i+1];
        }
        create(root);
        return root;
    }
}